package top.songxs.gmall.cart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.songxs.gmall.cart.entity.CartItem;
import top.songxs.gmall.cart.service.CartService;
import top.songxs.gmall.common.result.Result;
import top.songxs.gmall.common.result.ResultCodeEnum;

import java.util.List;

@RestController
@RequestMapping(value = "/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping(value = "/cartList")
    public Result<List<CartItem>> cartList() {
        List<CartItem> cartItemList = cartService.cartList();
        return Result.build(cartItemList, ResultCodeEnum.SUCCESS);
    }

    @PostMapping(value = "/addToCart/{skuId}/{skuNum}")
    public Result addCart(@PathVariable(value = "skuId") Long skuId, @PathVariable(value = "skuNum") Integer skuNum) {
        cartService.updateCartItem(skuId, skuNum);
        return Result.ok();
    }

    @GetMapping(value = "/checkCart/{skuId}/{ischecked}")
    public Result checkOut(@PathVariable(value = "skuId") Long skuId, @PathVariable(value = "ischecked") Integer ischecked) {
        cartService.checkCart(skuId, ischecked);
        return Result.ok();
    }

    @DeleteMapping(value = "/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable(value = "skuId") Long skuId) {
        cartService.deleteCartItem(skuId);
        return Result.ok();
    }


}